﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using DatabaseAdminTools.Connections;

namespace DatabaseAdminTools.OptionForms
{
    public partial class TableForm : Form
    {

        public static bool OPENED = false;

        public TableForm()
        {
            OPENED = true;
            InitializeComponent();
        }
        /*
         * Add a Table Tab to tab controls 
         * @param conn: Connection to get data from table
         * @param tableName: Table Name contains database name, example user@mysql
         */
        public void addTab(Connection conn, String tableName)
        {
            string tabText = tableName + " [" + conn.Name + "]";
            if (!checkSameTextTab(tabText))
            {
                try
                {
                    TableTab tab = new TableTab(conn, tableName);
                    tab.Dock = DockStyle.Fill;
                    TabPage page = new TabPage(tabText);
                    //page.ContextMenuStrip = contextMenuStrip1;
                    page.Controls.Add(tab);
                    tabControl1.TabPages.Add(page);
                    tabControl1.SelectedTab = page;
                    this.Text = "Tables: " + tabControl1.TabCount;
                }
                catch (Exception e)
                {
                    MessageBox.Show(this, e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    if (tabControl1.TabCount == 0)
                    {
                        this.Dispose();
                    }
                }
            }
        }
        // show confirmation, close all tab on tab control
        public void closeAllTab()
        {
            foreach(TabPage tab in tabControl1.TabPages)
            {
                ((TableTab)tab.Controls[0]).closeTab();
            }
        }

        /*
         * Remove a tab in tab control by tab name
         * @param tableName: Table Name contains database name, example user@mysql
         */
        public void removeTab(String tableName)
        {
            foreach (TabPage page in tabControl1.TabPages)
            {
                if (page.Text.Equals(tableName))
                {
                    tabControl1.TabPages.Remove(page);
                    break;
                }
            }
            if (tabControl1.TabPages.Count == 0)
            {
                this.Dispose();
            }
        }

        /*
         * Check all tabpage in tab controls, if found a same text tabpage, select it and return true
         * @param text: the text want to check
         * @return true if found a tabpage, else false
         */
        private bool checkSameTextTab(String text)
        {
            foreach (TabPage tab in tabControl1.TabPages)
            {
                if (tab.Text.Equals(text))
                {
                    tabControl1.SelectedTab = tab;
                    return true;
                }
            }
            return false;
        }

        private void TableView_Load(object sender, EventArgs e)
        {
            
        }
    }
}
